// 导入http模块
const http = require("http");
// 1.导入url模块
const url = require('url');

// 创建服务对象
const server = http.createServer((request, response) => {
  // 2.解析 request.url
  console.log(request.url);
  // 获取url中的查询字符串
  let res = url.parse(request.url,true) // true:将query解析成一个对象默认字符串
  console.log(res);
  /* Url {
  search: '?keyword=h5&num=1',
  query: [Object: null prototype] { keyword: 'h5', num: '1' },
  pathname: '/search',
  path: '/search?keyword=h5&num=1',
  href: '/search?keyword=h5&num=1'
} */
  // 获取url中的路径
  console.log(res.pathname);
  // 获取url中的查询字符串
  console.log(res.query);
  response.end("url");
});

// 3.监听端口，启动服务
server.listen(9000, () => {
  console.log("服务在9000端口启动");
});
